'''
Created on 2011-6-6

@author: elm
'''
import xmlrpclib,string
from ddbms.TrackerSite import TrackerSite
from ddbms.ExecuterSite import ExecuterSite
from ddbms.URIUtil import parseConn,parseUri
from ddbms.Json import read,dump
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SocketServer import ThreadingMixIn
class ThreadXMLRPCServer(ThreadingMixIn, SimpleXMLRPCServer):pass

class SiteServer:
    '''
    A running site 
    '''
    executionSite = None
    tracker = None
    uri = None
    trackerUri = None
    schema = None
    server = None
    mysql = None
    def __init__(self,uri,trackerUri,mysql,schema = None):
        '''
        Constructor
        '''
        self.uri = uri
        self.trackerUri = trackerUri
        self.schema = schema
        self.mysql = mysql
        
    def run(self):            
        self.tracker = xmlrpclib.ServerProxy(parseConn(self.trackerUri),allow_none=1)
        self.schema = read(self.tracker.join(self.uri, dump(self.schema), True))
        self.executionSite = ExecuterSite(self.schema,self.mysql)
        self.server = ThreadXMLRPCServer(parseUri(self.uri),allow_none=1)
        self.server.register_instance(self.executionSite)
        print( "Start site server at %s, tracker: %s"%(self.uri,self.trackerUri))
        self.server.serve_forever()
        
class TrackerServer:
    '''
    A running tracker
    '''
    uri = None
    server = None
    tracker = None
    def __init__(self, uri):
        '''
        '''
        self.uri = uri
        
    def run(self):
        self.tracker = TrackerSite()        
        self.server = ThreadXMLRPCServer(parseUri(self.uri),allow_none=1)
        self.server.register_instance(self.tracker)
        print( "Start tracker server at %s"%self.uri)
        self.server.serve_forever()

class Client:
    siteUri = None
    site = None
    debug = False
    def __init__(self, siteUri, debug = False):
        self.siteUri = siteUri
        self.debug = debug
        
    def run(self):
        self.site = xmlrpclib.ServerProxy(parseConn(self.siteUri),allow_none = 1)
        while True:
            try:
                sql =    raw_input ("sql>")
            except EOFError:
                break
            if not sql:
                continue
            if sql == "schema":
                schema = read(self.site.getSchema())
                print(str(schema))
            else:
                result = self.site.executeSql(sql,self.debug)
                if self.debug:
                    print ('''
Schema: %s

Sql: 
%s

Parsed Relational Algebra Expression:
%s

Unoptimized:
%s

Optimized:
%s

Execution time:
%s seconds

Result:
%s

                    '''%(result["schema"],result["sql"],result["beforeExpand"],result["beforeOptimize"],result["afterOptimize"],result["time"],result["result"]))
                else:
                    print (result)
            